package com.fone.player.activity.search.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.content.Context;
import android.content.res.ColorStateList;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.fone.player.R;
import com.fone.player.client.HotwordRst.Sk;

public class SearchGridAdapter extends BaseAdapter {

	private List<Sk> mList;
	private Context mContext;
	private List<Integer> colors = new ArrayList<Integer>();
	
	public SearchGridAdapter(Context context, List<Sk> list) {
		this.mContext = context;
		this.mList = list;
		colors.add(0xff46494b);
		colors.add(0xff06a1e4);
		colors.add(0xff799724);
		colors.add(0xfff56b0f);
		random = new Random();
	}

    /**
     * 创建text的ColorStateList
     * @param normal 	正常状态颜色  (16进制数字)
     * @param pressed	下按状态颜色	(16进制数字)
     * @return
     */
    private ColorStateList createColorStateList(int normal, int pressed) {  
        int[] colors = new int[] { pressed, normal, normal };  
        int[][] states = new int[3][];  
        states[0] = new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled };  
        states[1] = new int[] { android.R.attr.state_enabled };  
        states[2] = new int[] {};  
        ColorStateList colorList = new ColorStateList(states, colors);  
        return colorList;  
    }  
	
	@Override
	public int getCount() {
		int count = 0;
		if (mList != null) {
			count = mList.size();
		}
		return count;
	}

	@Override
	public Object getItem(int position) {

		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	ViewHolder viewHolder;
	private Random random;

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = View.inflate(mContext, R.layout.search_grid_item,
					null);
			viewHolder.name = (TextView) convertView
					.findViewById(R.id.tv_search_name);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		int color = colors.get(random.nextInt(4));
		viewHolder.name.setTextColor(createColorStateList(color,0xffffffff)); 
		Sk sk = mList.get(position);
		viewHolder.name.setText(sk.name);
		return convertView;
	}

	static class ViewHolder {
		public TextView name;
	}

}
